/**
 * Copyright 2014  XCL-Charts
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @Project XCL-Charts
 * @Description Android图表基类库
 * @author XiongChuanLiang<br   />(xcl_168@aliyun.com)
 * @license http://www.apache.org/licenses/  Apache v2 License
 * @version 2.1
 */
package com.droid.lib.controls.demo.widget;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.droid.lib.controls.chart.common.AngleEvaluator;
import com.droid.lib.controls.chart.event.click.BarPosition;
import com.droid.lib.controls.chart.renderer.FunnelChart;
import com.droid.lib.controls.chart.renderer.FunnelData;
import com.droid.lib.controls.chart.renderer.XEnum;
import com.droid.lib.controls.chart.view.ChartView;
import com.droid.lib.controls.widget.RadarPopItem;

import java.util.ArrayList;
import java.util.List;

import static com.droid.lib.controls.chart.renderer.XEnum.FunnelType.contrast;
import static com.droid.lib.controls.chart.renderer.XEnum.FunnelType.symmetry;

/**
 * @ClassName FunnelChartView
 * @Description 漏斗图
 *
 */
public class FunnelChartView extends ChartView{

    private String TAG = "FunnelChartView";
    private FunnelChart mChart;//基类
    private List<FunnelData> mOriginalData;//常用的数据 也可做对比的原始数据
    private List<FunnelData> mContrastData;//对比数据
    private Paint mPaintToolTip ;//绘画显示的弹窗
    private boolean isShowAnimation;//是否显示加载动画

    List<FunnelData> surData = new ArrayList<FunnelData>();
    List<FunnelData> anSurData = new ArrayList<FunnelData>();

    public FunnelChartView(Context context) {
        super(context);
        initView();
    }

    public FunnelChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public FunnelChartView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    public List<FunnelData> getmOriginalData() {
        return mOriginalData;
    }

    public FunnelChart getmChart() {
        return mChart;
    }

    public void setmChart(FunnelChart mChart) {
        this.mChart = mChart;
    }

    public void setmOriginalData(List<FunnelData> mOriginalData) {
        this.mOriginalData = mOriginalData;
        surData.clear();
        for(FunnelData  data :mOriginalData){
            FunnelData fun =new FunnelData( data.getKey(),data.getLabel(),data.getData(),data.getColor(),data.getAlpha());
            surData.add(fun);
        }
        mChart.setDataSource(surData);
        invalidate();
    }

    public List<FunnelData> getmContrastData() {
        return mContrastData;
    }

    public void setmContrastData(List<FunnelData> mContrastData) {
        this.mContrastData = mContrastData;
        anSurData.clear();
        for(FunnelData  data :mContrastData){
            FunnelData fun =new FunnelData( data.getKey(),data.getLabel(),data.getData(),data.getColor(),data.getAlpha());
            anSurData.add(fun);
        }
        mChart.setActuData(anSurData);
        invalidate();
    }

    public boolean isShowAnimation() {
        return isShowAnimation;
    }

    public void setShowAnimation(boolean showAnimation) {
        isShowAnimation = showAnimation;
    }

    private void initView() {
        mChart = new FunnelChart();
        mChart.setMvThisView(this);
        mPaintToolTip = new Paint(Paint.ANTI_ALIAS_FLAG) ;
        chartRender();
        chartAnimation();
//        if(isShowAnimation){
//        }else{
//            mChart.setDataSource(surData);
//            mChart.setActuData(mContrastData);
//        }
    }


    private void chartRender() {
        try {
            //设置绘图区默认缩进px值,留置空间显示Axis,Axistitle....
            int[] ltrb = getBarLnDefaultSpadding();
            mChart.setPadding(ltrb[0], ltrb[1], ltrb[2], ltrb[3]);
            mChart.showClikedFocus();
            mChart.setSortType(XEnum.SortType.DESC);
            mChart. ActiveListenItemClick();
            mChart.extPointClickRange(2);

            mChart.setLabelAlign(XEnum.HorizontalAlign.LEFT);

            mChart.syncLabelLineColor();

            mChart.setPlotWidthPercent(60.f);

            mChart.getFunnelLinePaint().setColor(Color.WHITE);
            mChart.getLabelPaint().setTextSize(34);

            mChart.setIsShowlegend(true);
            mChart.getPlotLegend().setmListenClick(true);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //图所占范围大小
        mChart.setChartRange(w, h);
    }

    @Override
    public void render(Canvas canvas) {
        try {
            mChart.render(canvas);

        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }


    private void chartAnimation() {

        try {
            for(int i=0;i< mOriginalData.size() ;i++){
                surData.add(mOriginalData.get(i));
                mChart.setDataSource(surData);
                if(anSurData!=null&&anSurData.size()>0){
                anSurData.add(mContrastData.get(i));
                mChart.setActuData(anSurData);
                postInvalidate();
                }
            }

        }
        catch(Exception e) {
            Thread.currentThread().interrupt();
        }
    }
    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        super.onTouchEvent(event);
        if(event.getAction() == MotionEvent.ACTION_UP)
        {
            triggerClick(event.getX(),event.getY());
        }
        return true;
    }

    //触发监听
    private void triggerClick(float x,float y)
    {
        //图例监听
        if(mChart.getPlotLegend().ismListenClick()){
            BarPosition record = mChart.getPlotLegend().getPositionRecord(x, y);
            if(null != record&&surData.get(record.getDataID()).getData()>0.f){
                FunnelData data = surData.get(record.getDataID());
                data.setData(0);
                data.setColor(Color.GRAY);
                data.setTextColor(Color.GRAY);
                mChart.setDataSource(surData) ;

                FunnelData compData = anSurData.get(record.getDataID());
                compData.setData(0);
                compData.setColor(Color.GRAY);
                compData.setTextColor(Color.GRAY);
                mChart.setActuData(anSurData);
                mChart.getToolTip().clean();
                postInvalidate();
                return;
            }
            if(null != record&&surData.get(record.getDataID()).getData()==0.f){
                surData.remove(record.getDataID());
                FunnelData fun =new FunnelData( mOriginalData.get(record.getDataID()).getKey(),mOriginalData.get(record.getDataID()).getLabel(),mOriginalData.get(record.getDataID()).getData(),mOriginalData.get(record.getDataID()).getColor(),mOriginalData.get(record.getDataID()).getAlpha());
                surData.add(record.getDataID(),fun);
                mChart.setDataSource(surData) ;

                anSurData.remove(record.getDataID());
                FunnelData anFun =new FunnelData( mContrastData.get(record.getDataID()).getKey(),mContrastData.get(record.getDataID()).getLabel(),mContrastData.get(record.getDataID()).getData(),mContrastData.get(record.getDataID()).getColor(),mContrastData.get(record.getDataID()).getAlpha());
                anSurData.add(record.getDataID(),anFun);
                mChart.setActuData(anSurData) ;
                mChart.getToolTip().clean();
                postInvalidate();
                return;
            }

        }


        if(mChart.getListenItemClickStatus()) {
            BarPosition record = mChart.getPositionRecord(x, y);
            if (null == record){
                mChart.getToolTip().clean();
                postInvalidate();
                return;
            }
            List<RadarPopItem> listRedarItems = new ArrayList<>();

            RadarPopItem item = new RadarPopItem();
            item.setColor(mChart.getmDataSet().get(record.getDataID()).getColor());
            item.setAlpha(mChart.getmDataSet().get(record.getDataID()).getAlpha());
            item.setId(1);
            item.setTittle(mChart.getmDataSet().get(record.getDataID()).getLabel());
            item.setName(mChart.getmDataSet().get(record.getDataID()).getKey());
            item.setValue(mChart.getmDataSet().get(record.getDataID()).getData());
            listRedarItems.add(item);
            if(mChart.getmFunType().equals(contrast)||mChart.getmFunType().equals(symmetry)){
                RadarPopItem itemAn = new RadarPopItem();
                itemAn.setColor(mChart.getActuData().get(record.getDataID()).getColor());
                itemAn.setAlpha(mChart.getActuData().get(record.getDataID()).getAlpha());
                itemAn.setId(2);
                itemAn.setTittle(mChart.getActuData().get(record.getDataID()).getLabel());
                itemAn.setName(mChart.getActuData().get(record.getDataID()).getKey());
                itemAn.setValue(mChart.getActuData().get(record.getDataID()).getData());
                listRedarItems.add(itemAn);
            }
            mChart.getToolTip().setCurrentXY(x, y);
            mChart.getToolTip().setListRedarItems(listRedarItems);
            postInvalidate();
            return;
        }
    }


}
